const api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
Page({
  data: {
    // 判断手机机型
    isIphoneX:0,
    starDesc: "",
    starNum: 0,
    fileList: [],
    pInfo:{},
    inputCon:""
  },
  comment(){
  },
  // 多行文本输入框失去焦点
  areaBlur(e){
    this.setData({
      inputCon:e.detail.value
    });
  },
  // 图片上传
  afterRead(event) {
    let that=this;
    const {file} = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'https://function.duolaibei.com/admin/upload/serviceUp', // 仅为示例，非真实的接口地址
      filePath: file.path,
      name: 'image',
      formData: { user: 'test' },
      success(res) {
        // 上传完成需要更新 fileList
        const data=JSON.parse(res.data)
        const { fileList = [] } = that.data;
        fileList.push({ ...file, url: data.content });
        that.setData({ fileList });
      },
    });
  },
  // 删除添加的图片
  deletePic(event) {
    let index = event.detail.index
    let arr = [];
    // 获取自定义数组数据
    var file = this.data.fileList;
    for (var i = 0; i < file.length; i++) {
      if (i != index) {
        arr.push(file[i]);
      }else{
        this.deleteImg(file[i].url)
      }
    }
    this.setData({
      fileList: arr
    });
  },
  // 删除指定src的图片
  deleteImg(src){
    wx.request({
      url: 'https://function.duolaibei.com/admin/upload/delService', //仅为示例，并非真实的接口地址
      data: {
          name:src
      },
      method:"POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
      }
    })
  },
  // 监听用户点击星星变化改变starDesc的值
  starOnChange(event) {
    let desc;
    switch (event.detail) {
      case 1:
        desc = "非常差";
        break;
      case 2:
        desc = "差";
        break;
      case 3:
        desc = "一般";
        break;
      case 4:
        desc = "好";
        break;
      case 5:
        desc = "非常好";
        break;
    }
    this.setData({
      starNum: event.detail,
      starDesc: desc
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取设备信息
    let phone = wx.getStorageSync('isIphoneX')
    if (phone) {
      this.setData({
        isIphoneX: 1
      })
    }
    let data=JSON.parse(options.info);
    this.setData({
      pInfo:data
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  submitComment(){
    let that=this;
    let inputCon=that.data.inputCon,starNum=that.data.starNum,fileList=that.data.fileList,pInfo=that.data.pInfo;
    if (starNum==0) {
      wx.showToast({
        title: '请为商品评级',
        icon:"none",
        duration:2000
      });
      return;
    }else if(inputCon<=0){
      wx.showToast({
        title: '请输入您的评价',
        icon:"none",
        duration:2000
      });
      return;
    }
    util.requestApi(api.setComment,{
      productID:pInfo.productID,
      orderID:pInfo.orderID,
      userID:pInfo.userID,
      toUserID:0,
      img:fileList,
      content:inputCon,
      star:starNum,
      type:0
    }).then((res)=>{
      if(res.code==200){
        wx.showToast({
          title: '评价成功',
          icon:"none",
          duration:2000
        });
        wx.navigateBack({
          delta: 2,
        })
      }
    })
  }
})